 /**
   * Copyright 2014 Viettel Telecom. All rights reserved.
   * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   */
 package com.viettel.core.dao.cms; 
import java.util.ArrayList; 
import java.util.List; 

import org.hibernate.type.StandardBasicTypes; 
import org.hibernate.type.Type; 
import org.springframework.beans.factory.annotation.Autowired; 

import com.viettel.core.exceptions.DataAccessException; 
import com.viettel.core.entities.cms.Role;
import com.viettel.core.entities.cms.RoleFunction;
import com.viettel.core.entities.vo.RoleFunctionVO;
import com.viettel.core.repo.IRepository;
 /**
   *  Mo ta muc dich cua lop (interface)
   *  @author: TOANTM1
   *  @version:  
   *  @since: 27/02/2014 - 10:50:59
   */
 public class RoleFunctionDAOImpl implements RoleFunctionDAO { 
	 private IRepository repo; 
		
	 	public void setRepo(IRepository repo) {
			this.repo = repo;
		} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 10:50:59
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public RoleFunction getRoleFunctionById(Integer id) throws DataAccessException { 
		return repo.getEntityById(RoleFunction.class, id); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 10:50:59
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public RoleFunction createRoleFunction(RoleFunction rolefunction) 
			throws DataAccessException { 
		if (rolefunction == null) { 
			throw new IllegalArgumentException("rolefunction"); 
		} 
		repo.create(rolefunction); 
		return repo.getEntityById(RoleFunction.class, rolefunction.getId()); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 10:50:59
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void updateRoleFunction(RoleFunction rolefunction) throws DataAccessException { 
		if (rolefunction == null) { 
			throw new IllegalArgumentException("rolefunction"); 
		} 
		repo.update(rolefunction); 
	} 
 
 	/**@author TOANTM1
  	 * 27/02/2014 - 10:50:59
  	 * For: 
  	 * @description: 
  	 */
 	@Override 
	public void deleteRoleFunction(RoleFunction rolefunction) throws DataAccessException { 
		if (rolefunction == null) { 
			throw new IllegalArgumentException("rolefunction"); 
		} 
		repo.delete(rolefunction); 
	}

	@Override
	public List<RoleFunctionVO> getAllRoleFunction() throws DataAccessException {
		List<Object> params = new ArrayList<Object>();
		StringBuilder selectQuery = new StringBuilder();
		StringBuilder fromQuery = new StringBuilder();
		StringBuilder countQuery = new StringBuilder();
		selectQuery.append("SELECT RF.ID as id, ");
		selectQuery.append("RF.name as roleName, ");
		selectQuery.append("RO.URL as url ");
		
		fromQuery.append("FROM ROLE_FUNCTION RF "); 
		fromQuery.append("JOIN ROLE RO ON RO.ID = RF.ROLE_ID "); 
		fromQuery.append("WHERE 1=1 ");
		
		selectQuery.append(fromQuery);		
		countQuery.append("SELECT COUNT(*) AS COUNT ").append(fromQuery);
		
		String[] fieldNames = new String[] { 
				"id", "roleName", "url"
				};
		Type[] fieldTypes = new Type[] { 
				StandardBasicTypes.INTEGER, StandardBasicTypes.STRING, StandardBasicTypes.STRING
				};

		List<Class<?>> synchronizedClass = new ArrayList<Class<?>>();
		synchronizedClass.add(RoleFunction.class);
		synchronizedClass.add(Role.class);
		
		List<RoleFunctionVO> list = null;
			list = repo.getListByQueryAndScalar(RoleFunctionVO.class,
					fieldNames, fieldTypes, selectQuery.toString(), params,
					synchronizedClass);
		return list;
	} 
	
	@Override
	public List<RoleFunction> getAllRolesFunction() throws DataAccessException {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT p.* ");
		sql.append(" FROM ROLE_FUNCTION p ");
		List<RoleFunction> list = repo.getListBySQL(RoleFunction.class, sql.toString(), null);
		return list;
	}
} 
 
